<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!--********************************************************************
* 该示例需要引入
* openlayers@4.6.5 (https://github.com/openlayers/openlayers/tree/v4.6.5)
* OL3AnimatedCluster (https://github.com/Viglino/OL3-AnimatedCluster)
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_Cluster"></title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <!-- 此范例基于 openlayers@4.6.5 版本 -->
    <script type="text/javascript" include="animatedclusterlayer,ol@4.6.5" src="../../dist/ol/include-ol.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript">
    var url = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-china/rest/maps/China_4326";
    new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        var map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
            view: new ol.View({
                center: [103, 30],
                zoom: 4,
                projection: 'EPSG:4326'
            })
        });
        var options = ol.source.TileSuperMapRest.optionsFromMapJSON(url, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);

        var clusterSource = new ol.source.Cluster({
            distance: 40,
            source: new ol.source.Vector(),
            wrapX: false
        });
        var clusterLayer = new ol.layer.AnimatedCluster({
            name: 'Cluster',
            source: clusterSource,
            animationDuration: 700,
            style: getStyle
        });
        map.addLayer(clusterLayer);

        function addFeatures(nb) {
            var features = [];
            var xmax = 130, xmin = 80, ymax = 50, ymin = 20;
            for (var i = 0; i < nb; ++i) {
                features[i] = new ol.Feature(new ol.geom.Point([Math.floor(Math.random() * (xmax - xmin + 1) + xmin), Math.floor(Math.random() * (ymax - ymin + 1) + ymin)]));
                features[i].set('id', i);
            }
            clusterSource.getSource().clear();
            clusterSource.getSource().addFeatures(features);
        }

        addFeatures(2000);


    });

    function getStyle(feature) {
        var styleCache = {};
        var size = feature.get('features').length;
        var style = styleCache[size];
        if (!style) {
            var color = size > 25 ? "192,0,0" : size > 8 ? "255,128,0" : "0,128,0";
            var radius = Math.max(8, Math.min(size * 0.75, 20));
            var dash = 2 * Math.PI * radius / 6;
            dash = [0, dash, dash, dash, dash, dash, dash];
            style = styleCache[size] = [new ol.style.Style({
                image: new ol.style.Circle({
                    radius: radius,
                    stroke: new ol.style.Stroke({
                        color: "rgba(" + color + ",0.5)",
                        width: 15,
                        lineDash: dash,
                        lineCap: "butt"
                    }),
                    fill: new ol.style.Fill({
                        color: "rgba(" + color + ",1)"
                    })
                }),
                text: new ol.style.Text({
                    text: size.toString(),
                    fill: new ol.style.Fill({
                        color: '#fff'
                    })
                })
            })
            ];
        }
        return style;
    }
</script>
</body>
</html>